package cn.tedu.net;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

public class TestIO {
    public static void main(String[] args) {
        method();//读一行
        method2();//写一行
        method3();//读一行写一行
    }

    public static void method3() {
        BufferedReader reader = null;
        PrintWriter writer = null;
        try {
            reader = new BufferedReader(new FileReader("D:\\1.txt"));
            writer = new PrintWriter("D:\\2.txt");
            while (true) {
                String s = reader.readLine();
                if (s == null) {
                    break;
                }
                writer.println(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                System.out.println("复制完毕...");
                reader.close();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void method2() {
        PrintWriter writer = null;
        try {
            writer = new PrintWriter("D:\\1.txt");
            writer.println("dsgagsadggsd");
            writer.println("上的嘎嘎d");
            writer.println("dsAG WERWETWEgsd");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("写完毕...");
            writer.close();
        }
    }

    //读一行
    public static void method() {
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader("D:\\1.txt"));
            while (true) {
                String s = reader.readLine();
                if (s == null) {
                    break;
                }
                System.out.println(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                System.out.println("**********分界线***********");
                System.out.println("读取完毕...");
                assert reader != null;
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
